package com.jqh.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jqh.demo.entity.AclMenu;
import com.jqh.demo.entity.AclRoleMenu;
import com.jqh.demo.service.AclMenuService;
import com.jqh.demo.service.AclRoleMenuService;
import com.jqh.demo.utils.Judge;
import com.jqh.demo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author jqh
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/api/acl-menu")
@CrossOrigin
public class AclMenuController {
    @Autowired
    private AclMenuService aclMenuService;

    @Autowired
    private AclRoleMenuService aclRoleMenuService;

    @ApiOperation("添加菜单")
    @PostMapping("addMenu")
    public R addMenu(@RequestBody AclMenu aclMenu){
        if(StringUtils.isEmpty(aclMenu.getMenuName())){
            Judge.judge(false,"菜单名称为空!");
        }
        if(StringUtils.isEmpty(aclMenu.getMenuUrl())){
            Judge.judge(false,"菜单路径为空!");
        }

        boolean flag = aclMenuService.save(aclMenu);
        return Judge.judge(flag,"添加失败！");
    }

    @ApiOperation("递归删除菜单")
    @DeleteMapping("{id}")
    public R delMenu(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            Judge.judge(false,"删除失败!");
        }
        boolean flag = aclMenuService.removeMenuAndChildrenById(id);
        return Judge.judge(flag,"删除失败！");
    }

    @ApiOperation("更新菜单")
    @PutMapping("updMenu")
    public R updMenu(@RequestBody AclMenu aclMenu){
        if(StringUtils.isEmpty(aclMenu.getId())){
            Judge.judge(false,"参数为空!");
        }
        boolean flag = aclMenuService.updateById(aclMenu);
        return Judge.judge(flag,"更新菜单失败！");
    }

    @ApiOperation("根据id查询菜单信息")
    @GetMapping("{id}")
    public R getMenuById(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            Judge.judge(false,"查询失败!");
        }
        AclMenu menu = aclMenuService.getById(id);
        return R.ok().data("menu",menu);
    }

    @ApiOperation("递归查询所有菜单")
    @GetMapping("getAllMenu")
    public R getAllMenu(){
        List<AclMenu> menuList=aclMenuService.getAllMenu();
        return R.ok().data("menuList",menuList);
    }
    

    @ApiOperation("给角色分配菜单")
    @PostMapping("giveRoleMenu")
    public R giveRoleMenu(@RequestParam String roleId,@RequestBody String[] menuIds){
        if(StringUtils.isEmpty(roleId)||menuIds.length==0){
            Judge.judge(false,"分配所需参数为空!");
        }
        aclMenuService.giveRoleMenu(roleId,menuIds);
        return R.ok();
    }

    @ApiOperation("根据角色id获取菜单")
    @GetMapping("getMenuByRoleId/{roleId}")
    public R getMenuByRoleId(@PathVariable String roleId){
        if(StringUtils.isEmpty(roleId)){
            Judge.judge(false,"id为空!");
        }
        List<AclMenu> aclMenus=aclMenuService.getMenuByRoleId(roleId);
        return R.ok().data("aclMenus",aclMenus);
    }

    @ApiOperation("根据角色id获取菜单id列表")
    @GetMapping("getMenuIds/{roleId}")
    public R getMenuIds(@PathVariable String roleId){
        if(StringUtils.isEmpty(roleId)){
            Judge.judge(false,"所需参数为空!");
        }
        List<AclRoleMenu> menuIds = aclRoleMenuService.list(new QueryWrapper<AclRoleMenu>().eq("role_id", roleId).select("menu_id"));
        List<String> ids=new ArrayList<>();
        for (AclRoleMenu menuId : menuIds) {
            ids.add(menuId.getMenuId());
        }
        return R.ok().data("menuIds",ids);
    }

    @ApiOperation("根据用户id查询可操作的菜单")
    @GetMapping("getAllOpeMenuByUserId/{userId}")
    public R getAllOpeMenuByUserId(@PathVariable String userId){
        List<AclMenu> menu=aclMenuService.getAllOpeMenuByUserId(userId);
        return R.ok().data("OpeMenu",menu);
    }

    @ApiOperation("未登录时展示默认菜单")
    @GetMapping("getDefaultMenu")
    public R getDefaultMenu(){
        List<AclMenu> defaultMenu=aclMenuService.getDefaultMenuList();
        return R.ok().data("defaultMenu",defaultMenu);
    }



}

